package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLSyntaxErrorException;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;


    //新增套餐
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //给套餐表添加数据
        Setmeal dish = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, dish);
        setmealMapper.insertmeal(dish);

        Long id = dish.getId();

        //给套餐内菜单表添加数据
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        if (!list.isEmpty()) {
            list.forEach(s -> s.setSetmealId(id));
            setmealDishMapper.insertmealwithdish(list);
        }
    }


    /**
     * 分页查询
     *
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id删除套餐
     */
    @Transactional
    public void deletequery(List<Long> ids) {

        //先判断此套餐状态，如果是起售中，就无法被删除
        for (Long id : ids) {   //在此对id这个集合进行循环，里面是一个id，一个id的方式进行套餐状态查询
            Setmeal setmeal = setmealMapper.getbyid(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) { //等于0说明在启售中，就不要删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //然后删除，删除套餐表
        setmealMapper.deletesetmeal(ids);
        //删除套餐关联的套餐内菜品表数据
        setmealDishMapper.deletemealdish(ids);

    }

    /**
     * 回显套餐的内容
     * @param id
     * @return
     */
    @Override
    public SetmealVO getbyid(Long id) {

        //先查找套餐的基本信息  除此外，还要查询套餐的分类名称
        Setmeal setmeal = setmealMapper.getbyids(id);

        //然后是菜品的基本信息
        List<SetmealDish> longList =  setmealDishMapper.getsetmealid(id);

        //放到vo中返回
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(longList);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        //dto中的套餐信息取出，覆盖原信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        //dto套餐内的菜品更改信息取出，覆盖原菜品，菜品也是，先全部删除，然后再新增
        List<Long> list = new ArrayList<>();
        list.add(setmealDTO.getId());
        setmealDishMapper.deletemealdish(list);

        Long id = setmeal.getId();

        List<SetmealDish> dishes = setmealDTO.getSetmealDishes(); //dto中的id  无法放到dishes数组中的setmealid中，解决了
        dishes.forEach(s -> s.setSetmealId(id));

        setmealDishMapper.insertmealwithdish(dishes); //依次插入套餐中的菜品数据

    }

    /**
     * 更改套餐状态
     * @param status
     * @param id
     */
    public void statusorstop(Integer status, Long id) {


        //先判断套餐内的菜品是否都在起售状态，如果有一个不是起售状态，则套餐无法被起售
        if(status == StatusConstant.ENABLE){
            List<SetmealDish> dishes = setmealDishMapper.getsetmealid(id);
            //拿到菜品id后查询菜品状态
            dishes.forEach(s -> {
                Dish dish = dishMapper.getbyid(s.getDishId());
                if (dish.getStatus() == StatusConstant.DISABLE){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });

        }

        Setmeal build = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(build);
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
